package cn.com.dhcc.ddi.node.service;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.dao.NodeInfoDao;
import cn.com.dhcc.ddi.node.vo.NodeInfo;
import cn.com.dhcc.ddi.node.vo.wapper.NodeInfoRowWapper;
/**
 * 
    * @ClassName: NodeInfoService
    * @Description: 节点Service
    * @author 18315
    * @date 2017-3-10
    *
 */
@Service
public class NodeInfoService extends BaseService {
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	@Autowired
	private NodeInfoDao nodeInfoDao;
		
	/**
	 * 获取节点信息列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */
	public PagerData<NodeInfo> list(Pager pager, WhereCondition condition) throws ServiceException {
		try {
			return nodeInfoDao.list(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取节点信息列表失败", e);
		}
	}
	
	
	
	/**
	    * 
	    * @Title: insert
	    * @Description: 更新节点信息
	    * @param @param vo
	    * @param @throws ServiceException
	    * @param @throws NiceException    参数
	    * @return void    返回类型
	    * @throws
	 */
	public int insert(NodeInfo vo) throws ServiceException {
		
		try {
			return nodeInfoDao.insert(vo);
		} catch (DaoException e)  {
			throw new ServiceException("新增失败", e);
		}
	}
  public int update(NodeInfo vo) throws ServiceException {
		
		try {
			return nodeInfoDao.update(vo);
		}catch (DaoException e)  {
			throw new ServiceException("更新失败", e);
		}
	}
	/**
	 * 
	    * @Title: insertOrUpdate
	    * @Description: 修改或者更新
	    * @param @param obj
	    * @param @throws DaoException    参数
	    * @return void    返回类型
	    * @throws
	 */
	public int insertOrUpdate(NodeInfo obj) throws ServiceException {
		try {
			 return nodeInfoDao.insertOrUpdate(obj);
		} catch (DaoException e) {
			throw new ServiceException("更新任务失败", e);
		}
	}
	
	
	/**
	 * 
	    * @Title: get
	    * @Description: 获取对象
	    * @param @param id
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return NodeInfo    返回类型
	    * @throws
	 */
	public NodeInfo get(String id) throws ServiceException {
		try {
			
		   return nodeInfoDao.get(id);
		} catch (DaoException e) {
			throw new ServiceException("根据id获取任务失败", e);
		}
		
	}
	
	/**
	 * 
	    * @Title: delete
	    * @Description: 删除
	    * @param @param status
	    * @param @param id
	    * @param @return    参数
	    * @return boolean    返回类型
	    * @throws
	 */
public List<NodeInfo> listnodeId(String id) throws DaoException {
		
		String sql = "SELECT a.id FROM " + SI_TABLE("t_ddi_node_info") + " a " +","
		
				+ SI_TABLE("t_ddi_datasource") + " b "+ " where a.id=? and a.id = b.node_id";
				
	
			return getDao().listModule("获取id列表",sql,NodeInfo.class,new Object[]{id});
		
	}
	
	public boolean deleteNodeInfo(String id) {
		boolean result = false;
		String sql = "delete from " + SI_TABLE("t_ddi_node_info") 
				+ " WHERE id=? ";
		try {
			result = CM.getDao().delete("删除", sql, new Object[]{id})> 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("删除失败", e);
		}
		return result;
	}
	
	
	/**
	 * 批量删除任务
	 * @param ids 任务id列表
	 * @return 成功个数
	 * @throws ServiceException
	 */
	public int batchDeleteByid(String[] ids) throws ServiceException {
		try {
			return nodeInfoDao.batchDelete(ids);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/**
	 * 验证名称唯一性
	 * @param name
	 * @param checkStr
	 * @return
	 * @throws ServiceException
	 */
	public boolean isPropertyUnique(String propertyName, String newValue, String oldValue) throws ServiceException {
		try {
			if (newValue.equals(oldValue)) {
				return true;
			}
			WhereCondition searchParameter = new WhereCondition();
			searchParameter.where().eq(propertyName, newValue);
			return nodeInfoDao.count(searchParameter) == 0;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	
		
	/**
	 * 根据id 停用或启用
	 * @param nodeId 节点id
	 * @param status 状态 0：停用  1：启用
	 * @return true 成功   false 失败
	 * @throws DaoException 
	 */
	public boolean del(String id, String status) {
		boolean result = false;
		String sql = "UPDATE " + SI_TABLE("t_ddi_node_info") + " SET status=?"
				+ " WHERE id=?";
		try {
			result = CM.getDao().update("更新node_info", sql, new Object[]{status, id}) > 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("更新node_info表失败", e);
		}
		return result;
	}
	
	/**
	 * 获取全部有效节点
	 * 
	 * @return
	 * @throws DaoException
	 */
	public List<NodeInfo> getAllNodeList(WhereCondition whereCondition) throws DaoException {
		try {
			final String sql = "select * from "
					+ SI_TABLE("t_ddi_node_info")
					+ " where  status <>'0'";
			return CM.getDao().list("获取全部有效节点", sql, new NodeInfoRowWapper(), whereCondition,
					new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("获取全部有效节点失败！", e);
		}
	}
	
	/**
	 * 
	    * @Title: getOrgCode
	    * @Description: 获取节点
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return List<NodeInfo>    返回类型
	    * @throws
	 */
	public List<NodeInfo> getOrgCode()
			throws ServiceException {
		try {
			return nodeInfoDao.getNodeInfo();
		} catch (DaoException e) {
			throw new ServiceException("获取节点失败", e);
		}
	}
	/**
	 * 
	    * @Title: getNodeOrgCode
	    * @Description: 获取省级行政区
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return List<NodeInfo>    返回类型
	    * @throws
	 */
	public List<NodeInfo> getNodeOrgCode()
			throws ServiceException {
		try {
			return nodeInfoDao.getTypeNodeInfo();
		} catch (DaoException e) {
			throw new ServiceException("获取行政区失败", e);
		}
	}
	
}
